﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class admin_dataPegawai : Form
    {
        public admin_dataPegawai()
        {
            InitializeComponent();
        }

        NhibernateHelper help = new NhibernateHelper();


        private void button4_Click(object sender, EventArgs e)
        {
            admin_MenuAwal menu = new admin_MenuAwal();
            menu.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var employee = new Pegawai
                    {
                       nama_pegawai = textBox2.Text,
                       username = textBox3.Text,
                       password = textBox4.Text
                    };

                    session.Save(employee);
                    transaction.Commit();
                    MessageBox.Show("Berhasil!");
                }
            }   
        }

        private void admin_dataPegawai_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Pegawai' table. You can move, or remove it, as needed.
            this.pegawaiTableAdapter.Fill(this.dotnetDataSet.Pegawai);
            // TODO: This line of code loads data into the 'cobaDataSetBaru1.Pegawai' table. You can move, or remove it, as needed.
            


        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;

            textBox1.Text = dataGridView1[0, i].Value.ToString();
            textBox2.Text = dataGridView1[1, i].Value.ToString();
            textBox3.Text = dataGridView1[2, i].Value.ToString();
            textBox4.Text = dataGridView1[3, i].Value.ToString();

            button1.Enabled = false;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            int id = Convert.ToInt32(textBox1.Text);

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var idPegawai = session.Get<Pegawai>(id);
                    idPegawai.nama_pegawai = textBox2.Text;
                    idPegawai.username = textBox3.Text;
                    idPegawai.password = textBox4.Text;
                    transaction.Commit();

                }
                MessageBox.Show("Berhasil!");
            }  
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var factory = help.CreateSessionFactory();

            using (var session = factory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var employee = new Pegawai
                    {
                        id_pegawai = int.Parse(textBox1.Text),
                        nama_pegawai = textBox2.Text,
                        username = textBox3.Text,
                        password = textBox4.Text
                    };

                    session.Delete(employee);
                    transaction.Commit();

                    MessageBox.Show("Data Berhasil Dihapus!");
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            
            
            
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            admin_tambahPegawai addPeg = new admin_tambahPegawai();
            addPeg.Show();
            this.Hide();
        }
    }
}
